package org.jeecg.common.modules.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.modules.sms.entity.Captchar;
import org.jeecg.common.modules.sms.mapper.CaptcharMapper;
import org.jeecg.common.modules.sms.service.ICaptcharService;
import org.jeecg.common.util.result.Result;
import org.jeecg.common.util.result.ResultMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @Description: captchar
 * @Author: jeecg-boot
 * @Date:   2020-08-22
 * @Version: V1.0
 */
@Service
public class CaptcharServiceImpl extends ServiceImpl<CaptcharMapper, Captchar> implements ICaptcharService {

    @Value("${user.login.captchar:214365}")
    private String captchar;
    @Override
    public Result checkMobileAndCode(String mobile, String code) {
        if(captchar.equals(code)){
            return ResultMapper.ok();
        }
        LambdaQueryWrapper<Captchar> query = Wrappers.lambdaQuery();  //定义写死：<类名称：动态改动>
        query.eq(Captchar::getMobile,mobile);  //对SQL语句的改写
        query.eq(Captchar::getCaptchar,code);
        Captchar captchar = getOne(query);  //声明类 = getOne(query)
        if(Objects.isNull(captchar)){
            return ResultMapper.<String>error().setMsg("验证码错误，请重新输入");
        }
        if(captchar.getExpire().before(new Date())){
            return ResultMapper.<String>error().setMsg("验证码已过期，请重新输入");
        }
        return ResultMapper.ok();
    }
}
